﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.tobyfreeman.utils {
    import flash.utils.*;

    public class Pool {

        private var pInstanceTable:Array;

        public function Pool(){
            pInstanceTable = [];
        }
        public function getInstanceCount(className:String):Number{
            if (pInstanceTable.hasOwnProperty(className)){
                return (pInstanceTable[className].length);
            };
            return (0);
        }
        public function returnInstance(obj:IPoolable):void{
            var objList:Array;
            if (obj == null){
                trace("/error/ Null parameter to Pool.returnInstance()");
                return;
            };
            obj.hibernate();
            var className:String = getQualifiedClassName(obj);
            if (pInstanceTable.hasOwnProperty(className)){
                pInstanceTable[className].push(obj);
            } else {
                pInstanceTable[className] = [obj];
            };
        }
        public function takeInstance(className:String):IPoolable{
            var obj:IPoolable;
            var classRef:Class;
            if (getInstanceCount(className) > 0){
                obj = IPoolable(pInstanceTable[className].pop());
            } else {
                classRef = (getDefinitionByName(className) as Class);
                obj = IPoolable(new (classRef));
            };
            return (obj);
        }

    }
}//package com.tobyfreeman.utils 
